﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using SIS.BLL;
using SIS.ENT;

public partial class school_PhotoUpload : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            //LoadSchoolList();
            ddlSchoolList.DataBind();
            if (Request.Params["id"] != null)
            {
                if (ddlSchoolList.Items.Count > 0)
                {
                    int _schoolId = 0;
                    if (Int32.TryParse(Request.Params["id"].ToString(), out _schoolId))
                    {
                        ddlSchoolList.Text = _schoolId.ToString();
                    }
                    else
                    {
                        ddlSchoolList.SelectedIndex = 0;
                    }
                    ddlSchoolList_SelectedIndexChanged(sender, e);
                }
            }
            else
            {
                if (ddlSchoolList.Items.Count > 0)
                {
                    ddlSchoolList.SelectedIndex = 0;
                    ddlSchoolList_SelectedIndexChanged(sender, e);
                }
            }

        }
    }

    protected void ddlSchoolList_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (ddlSchoolList.SelectedIndex >= 0)
        {
            SchoolProfileTable _profile = new SchoolProfileTable();
            _profile = SchoolProfileTableManager.GetItem(Convert.ToInt32(ddlSchoolList.Text));

            if (_profile != null)
            {
                if (_profile.SchoolLogo != null && _profile.SchoolLogo.Length > 0)
                {
                    imgCurrentSchoolLogo.ImageUrl = "~/images/imagehandler.ashx?key=" + _profile.SchoolKey.ToString() + "&type=schoollogo";
                    hplCurrentSchoolLogo.NavigateUrl = imgCurrentSchoolLogo.ImageUrl;
                    imgCurrentSchoolLogo.Width = 150;
                    imgCurrentSchoolLogo.Height = 75;
                }
                else
                {
                    imgCurrentSchoolLogo.ImageUrl = "";
                    hplCurrentSchoolLogo.NavigateUrl = "";
                }

                if (_profile.SchoolPhoto != null && _profile.SchoolPhoto.Length > 0)
                {
                    imgCurrentSchoolPhoto.ImageUrl = "~/images/imagehandler.ashx?key=" + _profile.SchoolKey.ToString() + "&type=schoolphoto";
                    hplCurrentSchoolPhoto.NavigateUrl = imgCurrentSchoolPhoto.ImageUrl;
                    imgCurrentSchoolPhoto.Width = 300;
                    imgCurrentSchoolPhoto.Height = 150;
                }
                else
                {
                    imgCurrentSchoolPhoto.ImageUrl = "";
                    hplCurrentSchoolPhoto.NavigateUrl = "";
                }
            }
            else
            {
                imgCurrentSchoolPhoto.ImageUrl = "";
                imgCurrentSchoolLogo.ImageUrl = "";
                imgCurrentSchoolLogo.Width = 0;
                imgCurrentSchoolLogo.Height = 0;
                imgCurrentSchoolPhoto.Width = 0;
                imgCurrentSchoolPhoto.Height = 0;
            }

            _profile = null;
        }
    }

    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        lblMessage.Text = "Upload failed!";

        int _fileLen = 0;
        string _fileName = string.Empty;
        string _fileExt = string.Empty;

        bool _fileExtValid = false;

        string[] _arrFileExt = System.Configuration.ConfigurationManager.AppSettings["SchoolImageUploadFileExt"].Split(',');
        int _LogoFileSize = Convert.ToInt32(System.Configuration.ConfigurationManager.AppSettings["SchoolUploadLogoFileSizeKB"]);
        int _PhotoFileSize = Convert.ToInt32(System.Configuration.ConfigurationManager.AppSettings["SchoolUploadPhotoFileSizeKB"]);

        if (fupLogo.HasFile && fupLogo.PostedFile != null)
        {
            if (fupLogo.PostedFile.ContentLength > 0)
            {
                _fileLen = fupLogo.PostedFile.ContentLength / 1024;
            }

            _fileName = fupLogo.PostedFile.FileName;
            _fileExt = System.IO.Path.GetExtension(_fileName).ToLower();

            for (int i = 0; i < _arrFileExt.Length; i++)
            {
                if (_fileExt == _arrFileExt[i])
                {
                    _fileExtValid = true;
                    break;
                }
            }

            if (_fileExtValid)
            {
                if (_fileLen > 0 && _fileLen <= _LogoFileSize)
                {
                    Byte[] imgLogo = null;
                    //To create a PostedFile
                    HttpPostedFile File = fupLogo.PostedFile;
                    //Create byte Array with file len
                    imgLogo = new Byte[File.ContentLength];
                    //force the control to load data in array
                    File.InputStream.Read(imgLogo, 0, File.ContentLength);

                    if (SchoolProfileTableManager.UpdateLogo(Convert.ToInt32(ddlSchoolList.Text), imgLogo))
                    {
                        lblMessage.Text = "Update logo succeeded!";
                        imgCurrentSchoolLogo.ImageUrl = "~/images/imagehandler.ashx?key=" + ddlSchoolList.Text + "&type=schoollogo";
                        hplCurrentSchoolLogo.NavigateUrl = imgCurrentSchoolLogo.ImageUrl;
                        imgCurrentSchoolLogo.Width = 150;
                        imgCurrentSchoolLogo.Height = 75;
                    }
                    else
                    {
                        lblMessage.Text = "Update logo failed!";
                    }
                    imgLogo = null;
                    File = null;
                }
                else
                {
                    lblMessage.Text += "<br/>Photo size is over limit!";
                }
            }
            else
            {
                lblMessage.Text += "<br/>Photo file type is NOT allowed to update!";
            }
        }
        else
        {
            lblMessage.Text = "No logo file was uploaded!";
        }

        _fileExt = string.Empty;
        _fileName = string.Empty;
        _fileExtValid = false;

        if (fupPhoto.HasFile && fupPhoto.PostedFile != null)
        {
            if (fupPhoto.PostedFile.ContentLength > 0)
            {
                _fileLen = fupPhoto.PostedFile.ContentLength / 1024;
            }

            _fileName = fupPhoto.PostedFile.FileName;
            _fileExt = System.IO.Path.GetExtension(_fileName).ToLower();

            for (int i = 0; i < _arrFileExt.Length; i++)
            {
                if (_fileExt == _arrFileExt[i])
                {
                    _fileExtValid = true;
                    break;
                }
            }

            if (_fileExtValid)
            {
                if (_fileLen > 0 && _fileLen <= _LogoFileSize)
                {
                    Byte[] imgPhoto = null;
                    //To create a PostedFile
                    HttpPostedFile File = fupPhoto.PostedFile;
                    //Create byte Array with file len
                    imgPhoto = new Byte[File.ContentLength];
                    //force the control to load data in array
                    File.InputStream.Read(imgPhoto, 0, File.ContentLength);

                    if (SchoolProfileTableManager.UpdatePhoto(Convert.ToInt32(ddlSchoolList.Text), imgPhoto))
                    {
                        lblMessage.Text += "<br/>Update photo succeeded!";
                        imgCurrentSchoolPhoto.ImageUrl = "~/images/imagehandler.ashx?key=" + ddlSchoolList.Text + "&type=schoolphoto";
                        hplCurrentSchoolPhoto.NavigateUrl = imgCurrentSchoolPhoto.ImageUrl;
                        imgCurrentSchoolPhoto.Width = 300;
                        imgCurrentSchoolPhoto.Height = 150;
                    }
                    else
                    {
                        lblMessage.Text += "<br/>Update photo failed!";
                    }
                    imgPhoto = null;
                    File = null;
                }
                else
                {
                    lblMessage.Text += "<br/>Photo size is over limit!";
                }
            }
            else
            {
                lblMessage.Text += "<br/>Photo file type is NOT allowed to update!";
            }
        }
        else
        {
            lblMessage.Text = "No photo file was uploaded!";
        }
    }

    protected void btnViewSchoolProfile_Click(object sender, EventArgs e)
    {
        Response.Redirect("profile.aspx");
    }
}
